const mongoose = require('mongoose');

mongoose.connect('mongodb://airbnb:web1903@liujiaxin.co:27017/airbnb', {
	useCreateIndex: true,
	useNewUrlParser: true,
	useUnifiedTopology: true
});

const UserSchema = new mongoose.Schema({
	// unique 给用户名设置唯一主键 防止同名注册
	username: { type: String, unique: true },
	password: {
		type: String,
		// 使用 bcrypt 包对密码进行同步加密
		set(val) {
			return require('bcrypt').hashSync(val, 10)
		}
	}
});
const User = mongoose.model('User', UserSchema);

// 删除集合 users
// User.db.dropCollection('users')

module.exports = { User };
